package com.yunhai.support.service.impl;

import com.yunhai.support.entity.vo.*;
import com.yunhai.support.service.WechatTemplateService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;


@Slf4j
@Service
public class WechatTemplateServiceImpl implements WechatTemplateService {

    @Resource
    private  WxMpService wxMpService;

    public void startWechatTemplate(WechatTemplateVo wechatTemplateVo) throws Exception {

        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData(wechatTemplateVo.getDeviceNameKey(),wechatTemplateVo.getDeviceName()),
                new WxMpTemplateData(wechatTemplateVo.getDeviceImeiKey(),wechatTemplateVo.getDeviceImei()),
                new WxMpTemplateData(wechatTemplateVo.getContentKey(),wechatTemplateVo.getContent()),
                new WxMpTemplateData(wechatTemplateVo.getAlarmTimeStrKey(),wechatTemplateVo.getAlarmTimeStr())
        );
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(wechatTemplateVo.getOpenId()) //openId
                .data(data) //数据
                .templateId(wechatTemplateVo.getTemplateId()) //模板id
                .build();

        //发送推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("{}用户的内容推送成功: {}", "", templateMessage );
        } catch ( Exception e ){
            log.info("{}用户的内容推送失败: {}", "", e);
        }

        return;
    }

    @Override
    public void startDeviceStatusTemplate(DeviceStatusTemplateVo deviceStatusVo) throws Exception {

        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData(deviceStatusVo.getDeviceNameKey(),deviceStatusVo.getDeviceName()),
                new WxMpTemplateData(deviceStatusVo.getDeviceImeiKey(),deviceStatusVo.getDeviceImei()),
                new WxMpTemplateData(deviceStatusVo.getContentKey(),deviceStatusVo.getContent()),
                new WxMpTemplateData(deviceStatusVo.getAlarmTimeStrKey(),deviceStatusVo.getAlarmTimeStr())
        );
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(deviceStatusVo.getOpenId()) //openId
                .data(data) //数据
                .templateId(deviceStatusVo.getTemplateId()) //模板id
                .build();

        //发送推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("{}用户的内容推送成功: {}", "", templateMessage );
        } catch ( Exception e ){
            log.info("{}用户的内容推送失败: {}", "", e);
        }

        return;
    }

    /**
     * 来点通知
     * */
    @Override
    public void startTrunOnElectricWechatTemplate(WechatTurnOnTemplateVo wechatTurnOnTemplateVo) throws Exception {
        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData(wechatTurnOnTemplateVo.getDeviceNameKey(),wechatTurnOnTemplateVo.getDeviceName()),
                new WxMpTemplateData(wechatTurnOnTemplateVo.getDeviceImeiKey(),wechatTurnOnTemplateVo.getDeviceImei()),
                new WxMpTemplateData(wechatTurnOnTemplateVo.getAlarmTimeStrKey(),wechatTurnOnTemplateVo.getAlarmTimeStr())
        );
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(wechatTurnOnTemplateVo.getOpenId()) //openId
                .data(data) //数据
                .templateId(wechatTurnOnTemplateVo.getTemplateId()) //模板id
                .build();

        //发送推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("来点通知： {}用户的内容推送成功: {}", "", templateMessage );
        } catch ( Exception e ){
            log.info("来点通知： {}用户的内容推送失败: {}", "", e);
        }

        return;
    }


    /**
     * 断电通知
     * */
    @Override
    public void startTrunOutElectricWechatTemplate(WechatTurnOutTemplateVo wechatTurnOutTemplateVo) throws Exception {
        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData(wechatTurnOutTemplateVo.getDeviceNameKey(),wechatTurnOutTemplateVo.getDeviceName()),
                new WxMpTemplateData(wechatTurnOutTemplateVo.getDeviceImeiKey(),wechatTurnOutTemplateVo.getDeviceImei()),
                new WxMpTemplateData(wechatTurnOutTemplateVo.getAlarmTimeStrKey(),wechatTurnOutTemplateVo.getAlarmTimeStr())
        );
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(wechatTurnOutTemplateVo.getOpenId()) //openId
                .data(data) //数据
                .templateId(wechatTurnOutTemplateVo.getTemplateId()) //模板id
                .build();

        //发送推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("断电通知：{}用户的内容推送成功: {}", "", templateMessage );
        } catch ( Exception e ){
            log.info("断电通知：{}用户的内容推送失败: {}", "", e);
        }

        return;
    }

    @Override
    public void startTestOnlineWechatTemplate(WechatTestTemplateVo wechatTestTemplateVo) throws Exception {
        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData(wechatTestTemplateVo.getDeviceNameKey(),wechatTestTemplateVo.getDeviceName()),
                new WxMpTemplateData(wechatTestTemplateVo.getDeviceImeiKey(),wechatTestTemplateVo.getDeviceImei())
        );
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(wechatTestTemplateVo.getOpenId()) //openId
                .data(data) //数据
                .templateId(wechatTestTemplateVo.getTemplateId()) //模板id
                .build();

        //发送推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("测试上线通知：{}用户的内容推送成功: {}", "", templateMessage );
        } catch ( Exception e ){
            log.info("测试上线通知：{}用户的内容推送失败: {}", "", e);
        }

        return;
    }

    @Override
    public void startWechatAudioTemplate(WechatAudioTemplateVo wechatAudioTemplateVo) throws Exception {
        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData(wechatAudioTemplateVo.getDeviceNameKey(),wechatAudioTemplateVo.getDeviceName()),
                new WxMpTemplateData(wechatAudioTemplateVo.getDeviceImeiKey(),wechatAudioTemplateVo.getDeviceImei()),
                new WxMpTemplateData(wechatAudioTemplateVo.getContentKey(),wechatAudioTemplateVo.getContent()),
                new WxMpTemplateData(wechatAudioTemplateVo.getAlarmTimeStrKey(),wechatAudioTemplateVo.getAlarmTimeStr())
        );
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(wechatAudioTemplateVo.getOpenId()) //openId
                .data(data) //数据
                .templateId(wechatAudioTemplateVo.getTemplateId()) //模板id
                .build();

        //发送推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("断电通知：{}用户的内容推送成功: {}", "", templateMessage );
        } catch ( Exception e ){
            log.info("断电通知：{}用户的内容推送失败: {}", "", e);
        }

        return;
    }

    @Override
    public void startDeviceannualChargeWechatTemplate(WechatAnnualChargeTemplateVo wechatAnnualChargeTemplateVo) throws Exception {
        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData(wechatAnnualChargeTemplateVo.getDeviceNameKey(),wechatAnnualChargeTemplateVo.getDeviceName()),
                new WxMpTemplateData(wechatAnnualChargeTemplateVo.getDeviceImeiKey(),wechatAnnualChargeTemplateVo.getDeviceImei()),
                new WxMpTemplateData(wechatAnnualChargeTemplateVo.getContentKey(),wechatAnnualChargeTemplateVo.getContent())
        );

        String redirectUrl1 = URLEncoder.encode("http://m.tjwl77.com/#/autologin", "UTF-8");
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5de148444923d15f&scope=snsapi_base&state=3&connect_redirect=1&response_type=code&redirect_uri="+redirectUrl1+"#wechat_redirect";

        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(wechatAnnualChargeTemplateVo.getOpenId()) //openId
                .data(data) //数据
                .url(url)
                .templateId(wechatAnnualChargeTemplateVo.getTemplateId()) //模板id
                .build();

        //发送推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("续费通知：{}用户的内容推送成功: {}", "", templateMessage );
        } catch ( Exception e ){
            log.info("续费通知：{}用户的内容推送失败: {}", "", e);
        }

        return;
    }

}
